package com.example.boos.stores.money;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.example.boos.HttpUtil.MyPost;
import com.example.boos.R;
import com.example.boos.context.BossInfo;
import com.example.boos.pojo.Orders;
import com.example.boos.util.MyThreadPool;

import java.util.ArrayList;
import java.util.List;

public class BillsFragment extends Fragment {
    private BillsFragmentCallBack callBack;
    private balance_main act;
    private View view;
    private RecyclerView bills;
    private BillsItemAdapter adapter;

    private List<Orders> orders;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 1:
                    if((Boolean)msg.obj){
                        adapter.updateAll(orders);
                    }
                    break;
            }
        }
    };

    public BillsFragment(BillsFragmentCallBack callBack,balance_main act){
        this.callBack = callBack;
        this.act = act;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_balance_bill,container,false);
        initData();
        initView(view);
        return view;
    }

    private void initView(View view){
        bills = view.findViewById(R.id.bill_bills);
        if(orders == null){
            orders = new ArrayList<>();
        }
        adapter = new BillsItemAdapter(act,orders);
        bills.setAdapter(adapter);
        bills.setLayoutManager(new GridLayoutManager(getContext(),1));
    };

    private void initData(){
        // 获取到骑手的已完成订单信息
        MyThreadPool.doJob(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                orders = (List<Orders>) MyPost.doPost("getOrderByBossIdAndUserStatus", BossInfo.bossId);
                msg.obj = true;
                if(orders == null){
                    msg.obj = false;
                }
                msg.what = 1;
                handler.sendMessage(msg);
            }
        });
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.back_menu1,menu);
        for(int i=0;i<menu.size()-1;i++){
            menu.getItem(i).setEnabled(false);
            menu.getItem(i).setVisible(false);
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        // 账单详细返回到 我的钱包页面
        callBack.back();
        return true;
    }
}
